package uvt.gui.mainframe.subpanel.addsss;

import java.awt.Color;

import javax.swing.JButton;

import org.openstreetmap.gui.jmapviewer.Coordinate;

import uvt.database.Database;
import uvt.database.InsertOperations;
import uvt.database.SelectOperations;
import uvt.gui.mainframe.object.InfoFrame;
import uvt.gui.mainframe.object.command.Command;
import uvt.gui.mainframe.object.observer.ConcreteObservable;
import uvt.map.PositionsSettings;
import uvt.map.graph.element.Segment;
import uvt.map.graph.element.Station;
import uvt.map.mapmarker.MapPolygonImpl;

public class CompleteRouteButton extends JButton implements Command {
	
	private Validable validate;
	private TextFieldGetter getter;
	
	public CompleteRouteButton(Validable validate,TextFieldGetter getter) {
		super("Complete Route");
		this.validate = validate;
		this.getter = getter;
	}
	
	public void execute() {
		try {
			validate.validateForCompleteRouteButton();
			
			addSegment();
			addStreetName();
			addRoadAndRoute();
			
			ConcreteObservable.getStreetNameObservableInstance().stateChanged();
			CoordinateResources.getInstance().getListSegments().clear();
			PositionsSettings.getInstance().cleanMapMarkersUsed();
			Thread t = new Thread(new Runnable() {
				public void run() {
					new InfoFrame("Information","Route has been completed and added in database!");
			    }
			});
			t.start();
		} catch(final Exception ex) {
			Thread t = new Thread(new Runnable() {
				public void run() {
					new InfoFrame("Error",ex.getMessage());
				}
			});
			t.start();
		}
		
	}
	
	private void addSegment() {
	   // add the segment
       Coordinate firstCoord = CoordinateResources.getInstance().getSegmentStart();
       Station st = CoordinateResources.getInstance().getFirstStation();
	   CoordinateResources.getInstance().addSegmetInList(new Segment(firstCoord.getLat(),
																     firstCoord.getLon(),
																     st.getLatitude(),
																     st.getLongitude()));
		((InsertOperations)Database.getInstace()).insertRowStreetSegment(String.valueOf(firstCoord.getLat()), 
													                     String.valueOf(firstCoord.getLon()),
													                     String.valueOf(st.getLatitude()),
													                     String.valueOf(st.getLongitude()));
		MapPolygonImpl.setPolygonColor(Color.yellow);
		PositionsSettings.getInstance().addSegmentOnMap(getter.getStreetName(),
				                                        new Segment(firstCoord.getLat(),
						                                            firstCoord.getLon(),
						                                            st.getLatitude(),
																    st.getLongitude()));
	}
	
	private void addStreetName() {
		// add the street name
		((InsertOperations)Database.getInstace()).insertRowStreetName(getter.getStreetName());
		int idStreetName = ((SelectOperations)Database.getInstace()).selectIdStreetName(getter.getStreetName());
		for(Segment seg : CoordinateResources.getInstance().getListSegments()) {
			int idStrSeg = ((SelectOperations)Database.getInstace()).selectIdStreetSegment(String.valueOf(seg.getxA()),
					                                                                       String.valueOf(seg.getxB()),
					                                                                       String.valueOf(seg.getyA()),
					                                                                       String.valueOf(seg.getyB()));
			((InsertOperations)Database.getInstace()).insertRowStreet(idStrSeg,idStreetName);
		}			
	}
	
	private void addRoadAndRoute() {
		// add the road
		Station station = CoordinateResources.getInstance().getFirstStation();
		Station st = CoordinateResources.getInstance().getStationStart();
		int idFrom = ((SelectOperations)Database.getInstace()).selectIdStation(st.getName(),
				                                                               String.valueOf(st.getLatitude()),
				                                                               String.valueOf(st.getLongitude()));
		int idStreetName = ((SelectOperations)Database.getInstace()).selectIdStreetName(getter.getStreetName());
		int idTo = ((SelectOperations)Database.getInstace()).selectIdStation(station.getName(),
																			 String.valueOf(station.getLatitude()),
														                     String.valueOf(station.getLongitude()));
		((InsertOperations)Database.getInstace()).insertRowRoad(idFrom,idStreetName,idTo);
		// add the route
		int idRoad = ((SelectOperations)Database.getInstace()).selectIdRoad(idFrom,idStreetName,idTo);
		int idRouteName = ((SelectOperations)Database.getInstace()).selectIdRouteName(getter.getRouteName());
		((InsertOperations)Database.getInstace()).insertRowRoute(idRouteName, idRoad);
	}
	
}